import pandas as pd


def error10_idf(df,dict_info_setting, columns):
    """
    处理健康建议相关字段的逻辑错误，并记录样式和批注信息。

    :param df: 数据 DataFrame
    :param columns: 包含8个列名的列表，顺序为 [
        '吸烟量', '建议日吸烟量(支/天)',
        '日饮酒量(两)', '建议日饮酒量(两/天)',
        '每周运动次数(次)', '每次运动时间(分钟/次)',
        '建议每周运动次数(次)', '建议每次运动时间(分钟/次)'
    ]
    :return: (styles_info, comments_info) 样式和批注信息
    """
    styles_info = {}
    comments_info = []

    T_F = dict_info_setting["error10_idf"].split(":")[-1].strip()

    # 解包列名
    col_smoke, col_smoke_adv, col_drink, col_drink_adv, \
        col_exercise_cnt, col_exercise_time, col_exercise_cnt_adv, col_exercise_time_adv = columns

    for row_idx, row in df.iterrows():
        # 处理吸烟量逻辑
        smoke = row[col_smoke]
        smoke_adv = row[col_smoke_adv]
        if pd.notna(smoke) and pd.notna(smoke_adv):
            if 0 < smoke <= smoke_adv:
                # 标黄两列
                styles_info[(row_idx, col_smoke)] = 'warning'
                styles_info[(row_idx, col_smoke_adv)] = 'warning'
                msg = f"实际吸烟量({smoke}支/天)未超过建议量({smoke_adv}支/天)，请确认是否需要调整建议值"
                comments_info.extend([(row_idx, col_smoke, msg), (row_idx, col_smoke_adv, msg)])

        # 处理饮酒量逻辑
        drink = row[col_drink]
        drink_adv = row[col_drink_adv]
        if pd.notna(drink) and pd.notna(drink_adv):
            if 0 < drink <= drink_adv:
                styles_info[(row_idx, col_drink)] = 'warning'
                styles_info[(row_idx, col_drink_adv)] = 'warning'
                msg = f"实际饮酒量({drink}两/天)未超过建议量({drink_adv}两/天)，请确认是否需要调整建议值"
                comments_info.extend([(row_idx, col_drink, msg), (row_idx, col_drink_adv, msg)])

        # 处理运动量超建议值逻辑
        ex_cnt = row[col_exercise_cnt]
        ex_time = row[col_exercise_time]
        ex_cnt_adv = row[col_exercise_cnt_adv]
        ex_time_adv = row[col_exercise_time_adv]
        if all(pd.notna([ex_cnt, ex_time, ex_cnt_adv, ex_time_adv])):
            actual = ex_cnt * ex_time
            suggest = ex_cnt_adv * ex_time_adv
            if actual > suggest:
                # 标黄所有运动相关列
                for col in [col_exercise_cnt, col_exercise_time, col_exercise_cnt_adv, col_exercise_time_adv]:
                    styles_info[(row_idx, col)] = 'warning'
                msg = f"实际运动量({actual}分钟/周)超过建议量({suggest}分钟/周)"
                comments_info.extend([
                    (row_idx, col_exercise_cnt, msg),
                    (row_idx, col_exercise_time, msg),
                    (row_idx, col_exercise_cnt_adv, msg),
                    (row_idx, col_exercise_time_adv, msg)
                ])

        # 处理实际运动次数与时间冲突
        ex_cnt = row[col_exercise_cnt]
        ex_time = row[col_exercise_time]
        if pd.notna(ex_cnt) and pd.notna(ex_time):
            if (ex_cnt == 0 and ex_time != 0) or (ex_cnt != 0 and ex_time == 0):
                for col in [col_exercise_cnt, col_exercise_time]:
                    styles_info[(row_idx, col)] = 'warning'
                msg = "实际运动次数与时间应同时为0或同时不为0"
                comments_info.extend([(row_idx, col_exercise_cnt, msg), (row_idx, col_exercise_time, msg)])

        # 处理建议运动次数与时间冲突
        ex_cnt_adv = row[col_exercise_cnt_adv]
        ex_time_adv = row[col_exercise_time_adv]
        if pd.notna(ex_cnt_adv) and pd.notna(ex_time_adv):
            if (ex_cnt_adv == 0 and ex_time_adv != 0) or (ex_cnt_adv != 0 and ex_time_adv == 0):
                for col in [col_exercise_cnt_adv, col_exercise_time_adv]:
                    styles_info[(row_idx, col)] = 'warning'
                msg = "建议运动次数与时间应同时为0或同时不为0"
                comments_info.extend([(row_idx, col_exercise_cnt_adv, msg), (row_idx, col_exercise_time_adv, msg)])

    if T_F == "True":
        return styles_info, comments_info
    else:
        return {}, []
